package dao;

import model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

public class UserDao {
    public User selectByUsernamePassword(String account,String password)throws SQLException{
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from user where account=? and password=?";
        return  r.query(sql,new BeanHandler<User>(User.class),account,password);
    }
    public User selectByEmailPassword(String account,String password)throws SQLException{
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from user where account=? and password=?";
        return  r.query(sql,new BeanHandler<User>(User.class),account,password);
    }
//    Date date = new Date();
public User selectById(int id) throws SQLException{
        QueryRunner r =new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from user where id=?";
        return r.query(sql,new BeanHandler<User>(User.class),id);
}
public void updateUserAddress(User users) throws SQLException{
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql ="update user set account=?,password=?,phone=?,email=? where id=?";
        System.out.println(users.getPassword());
       r.update(sql,users.getAccount(),users.getPassword(),users.getPhone(),users.getEmail(),users.getId());
}

    public void addUser(User users) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "insert into user(account,password,email,phone) values(?,?,?,?)";
        r.update(sql, users.getAccount(), users.getPassword(), users.getEmail(), users.getPhone());
    }
    public void delete(int id)throws SQLException{
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="delete from user where id=?";
        r.update(sql,id);
    }
    public boolean isUsernameExist(String username)throws SQLException{
        QueryRunner r =new QueryRunner(DataSourceUtils.getDataSource());
        String sql ="select * from user where account = ?";
        User u =r.query(sql,new BeanHandler<User>(User.class),username);
        if (u==null){
            return false;
        }else {
            return true;
        }
    }
    public boolean isEmailExist(String email) throws SQLException{
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where email=?";
        User u=(User)r.query(sql,new BeanHandler(User.class),new Object[]{email});
        return u !=null;
    }
    public boolean isPhone(String phone) throws SQLException{
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where phone=?";
        User u=(User)r.query(sql,new BeanHandler(User.class),new Object[]{phone});
        return u !=null;
    }
    public List<User> getAllUser() throws SQLException{
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user";
        return r.query(sql,new BeanListHandler<User>(User.class));
    }
}
